package org.grow.mybatisgo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.grow.mybatisgo.dao.DealBillDao;
import org.grow.mybatisgo.dao.StudentScoreDao;
import org.grow.mybatisgo.entity.DealBill;
import org.grow.mybatisgo.entity.QueryRequest;
import org.grow.mybatisgo.entity.QueryResponse;
import org.grow.mybatisgo.entity.StudentScore;
import org.grow.mybatisgo.service.DealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller

@CrossOrigin
public class ScoreController {

    @Autowired
    private StudentScoreDao studentScoreDao;

    @PutMapping("/studentScore")
    @ResponseBody
    public Boolean insert(@RequestBody StudentScore studentScore){
        int i = studentScoreDao.insert(studentScore);
        return i == 1 ;
    }
    @PostMapping("/studentScore/query")
    @ResponseBody
    public QueryResponse query(@RequestBody QueryRequest queryRequest){
        System.out.println(queryRequest);
        IPage<StudentScore> page =
                new Page<>(queryRequest.getPageNum(),queryRequest.getPageSize());
        IPage<StudentScore> page1 = studentScoreDao.
                dynamicQueryPaged(page, queryRequest.getQueryCondition());
        List<StudentScore> records = page1.getRecords();
        System.out.println(records);

        QueryResponse queryResponse = new QueryResponse();
        queryResponse.setResults(records);
        queryResponse.setTotal((int)page1.getTotal());
        return queryResponse;


    }

//    post deal
    /*{
                    dealTime:null,
                    supplierName:"",
                    consumerName:"",
                    dealScore:5,

                }*/

    @Autowired
    private DealBillDao dealBillDao;
//    @PostMapping("/studentScore/deal")
//    @ResponseBody
//    public Boolean deal(@RequestBody DealBill dealBill){
//
//        StudentScore supplier = studentScoreDao.findByName(dealBill.getSupplierName());
//        StudentScore consumer = studentScoreDao.findByName(dealBill.getConsumerName());
//        if (supplier ==null || consumer == null ) return false;
//        Float supplierOnlineScore = supplier.getOnlineScore()-dealBill.getDealScore();
//        Float consumerOnlineScore = consumer.getOnlineScore()+dealBill.getDealScore();
//        if (supplierOnlineScore<0) return false;
//
//        dealBill.setDealBillId(0);
//        dealBillDao.insert(dealBill);
//
//        studentScoreDao.updateScoreByName(supplierOnlineScore,dealBill.getSupplierName());
//        studentScoreDao.updateScoreByName(consumerOnlineScore,dealBill.getConsumerName());
//
//        return true;
//
//    }
    @Autowired
    private PlatformTransactionManager platformTransactionManager;
//    @PostMapping("/studentScore/deal")
//    @ResponseBody
//    public Boolean deal(@RequestBody DealBill dealBill){
//
//        StudentScore supplier = studentScoreDao.findByName(dealBill.getSupplierName());
//        StudentScore consumer = studentScoreDao.findByName(dealBill.getConsumerName());
//        if (supplier ==null || consumer == null ) return false;
//        Float supplierOnlineScore = supplier.getOnlineScore()-dealBill.getDealScore();
//        Float consumerOnlineScore = consumer.getOnlineScore()+dealBill.getDealScore();
//        if (supplierOnlineScore<0) return false;
//
//        dealBill.setDealBillId(0);
//        dealBillDao.insert(dealBill);
//        TransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
//        System.out.println("tx:?  " +(platformTransactionManager==null));
//        TransactionStatus status = platformTransactionManager.getTransaction(transactionDefinition);
//        try{
//
//            studentScoreDao.updateScoreByName(supplierOnlineScore,dealBill.getSupplierName());
//            studentScoreDao.updateScoreByName(consumerOnlineScore,dealBill.getConsumerName());
//
//            platformTransactionManager.commit(status);
//        }catch (Exception e){
//            platformTransactionManager.rollback(status);
//            return false;
//        }finally {
//            System.out.println("结束事务");
//        }
//
//        return true;
//
//    }

    @Autowired
    private DealService dealService;

    @PostMapping("/studentScore/deal")
    @ResponseBody
    public Boolean deal2(@RequestBody DealBill dealBill){

        StudentScore supplier = studentScoreDao.findByName(dealBill.getSupplierName());
        StudentScore consumer = studentScoreDao.findByName(dealBill.getConsumerName());
        if (supplier ==null || consumer == null ) return false;
        Float supplierOnlineScore = supplier.getOnlineScore()-dealBill.getDealScore();
        Float consumerOnlineScore = consumer.getOnlineScore()+dealBill.getDealScore();
        if (supplierOnlineScore<0) return false;

        dealBill.setDealBillId(0);
        dealBillDao.insert(dealBill);


        return dealService.txDeal(dealBill,supplierOnlineScore,consumerOnlineScore);
//            studentScoreDao.updateScoreByName(supplierOnlineScore,dealBill.getSupplierName());
//            studentScoreDao.updateScoreByName(consumerOnlineScore,dealBill.getConsumerName());
//
//
//        return true;

    }
}
